package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: MediaFileProcessServiceImpl
 * Package: com.xuecheng.media.service.impl
 * Description:
 *
 * @Author Endeavor-ZQB
 * @Create 2023/2/8 17:00
 * @Version 1.0
 */
@Service
@Slf4j
public class MediaFileProcessServiceImpl implements MediaFileProcessService {

    @Autowired
    MediaFilesMapper mediaFilesMapper;

    @Autowired
    MediaProcessMapper mediaProcessMapper;

    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;


    @Override
    public List<MediaProcess> getMediaProcessList(int shardTotal, int shardIndex, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
        return mediaProcesses;
    }

    @Transactional
    @Override
    public void saveProcessFinishStatus(int status, String fileId, String url, String errorMsg) {

        //根据id查询视频是否在视频处理表中
        LambdaQueryWrapper<MediaProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MediaProcess::getFileId,fileId);
        MediaProcess mediaProcess = mediaProcessMapper.selectOne(queryWrapper);

        if (mediaProcess == null){
           log.debug("更新任务状态时，此时任务:{}，为空",mediaProcess.getId());
           return;
        }

        //处理失败
        if (status==3){
            mediaProcess.setStatus("3");
            mediaProcess.setErrormsg(errorMsg);
            mediaProcessMapper.updateById(mediaProcess);
            XueChengPlusException.cast("视频处理失败");
        }


        //如果处理视频成功，则设置URL和状态
        if (status==2){
            mediaProcess.setUrl(url);
            mediaProcess.setStatus("2");
            mediaProcess.setFinishDate(LocalDateTime.now());
            mediaProcessMapper.updateById(mediaProcess);
        }
     ;

        //上传处理后的视频文件向media_files插入记录
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (mediaFiles !=null ){
            mediaFiles.setUrl(url);
            mediaFilesMapper.updateById(mediaFiles);
        }

        //处理视频成功 插入记录到 处理视频历史表中
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
        mediaProcessHistoryMapper.insert(mediaProcessHistory);

        //处理视频成功 视频处理表中删除
        mediaProcessMapper.deleteById(mediaProcess.getId());


    }
}
